package com.example.syyao.myapp.Adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.syyao.myapp.R;
import com.example.syyao.myapp.Response.Image;
import com.example.syyao.myapp.Tool.Picture_tool;
import com.w4lle.library.NineGridAdapter;
import com.w4lle.library.NineGridlayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NVAdapter extends BaseAdapter {
    public Context context;
    public List<List<Image>> datalist;
    public NineGridAdapter adapter;
    public ImageView imageView;
    public   List<Map<String,Object>> myinformation;

    private TextView nickname;
    private TextView time;
    private ImageView head_picture;
    private TextView tag1;
    private TextView tag2;

    public NVAdapter(Context context, List<List<Image>> datalist, List<Map<String,Object>> information) {
        this.context = context;
        this.datalist = datalist;
        this.myinformation = information;

    }
    public NVAdapter(Context context, List<List<Image>> datalist) {
        this.context = context;
        this.datalist = datalist;
    }

    public void update(List<List<Image>> datalist, List<Map<String,Object>> information) {
        this.datalist = datalist;
        this.myinformation = information;
    }
    public void update(Context context,List<List<Image>> datalist, List<Map<String,Object>> information){

        this.context=context;
        this.datalist = datalist;
        this.myinformation = information;
        System.out.println(information.get(0));
        System.out.println(information.get(1));
        this.notifyDataSetChanged();

    }

    @Override
    public int getCount() {
        return datalist.size();
    }

    @Override
    public Object getItem(int position) {
        return datalist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        System.out.println(position);
        List<Image> itemList = datalist.get(position);
//        System.out.println("NVA"+itemList);
        Map<String,Object> temp = new HashMap<>();

        temp = myinformation.get(position);
//        System.out.println("NVA"+temp);
//
//        if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.item_ninegridlayout, parent, false);
        nickname = convertView.findViewById(R.id.nickname);
        time = convertView.findViewById(R.id.time);
        head_picture = convertView.findViewById(R.id.head_picture);
        tag1 = convertView.findViewById(R.id.tag1);
        nickname.setText(temp.get("nickname").toString());
        time.setText(temp.get("time").toString());
        tag1.setText(temp.get("tagname").toString());
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.login_pic)		;
        Glide.with(context).asBitmap().load("http://120.79.205.71/picture/"+temp.get("head_picture").toString()).apply(options).into(head_picture);
//            tag2.setText(temp.get("tagname[1]").toString());
        viewHolder = new ViewHolder();
        viewHolder.ivMore = (NineGridlayout) convertView.findViewById(R.id.iv_ngrid_layout);

        convertView.setTag(viewHolder);
//        } else {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
        if (itemList.isEmpty()) {
            viewHolder.ivMore.setVisibility(View.GONE);
        } else {
            viewHolder.ivMore.setVisibility(View.VISIBLE);
            handlerOneImage(viewHolder, itemList);
        }

        return convertView;
    }

    private void handlerOneImage(ViewHolder viewHolder, List<Image> image) {
        adapter = new Adapter(context, image);
        viewHolder.ivMore.setAdapter(adapter);
        viewHolder.ivMore.setOnItemClickListerner(new NineGridlayout.OnItemClickListerner() {
            @Override
            public void onItemClick(View view, int position) {
                //do some thing
                Log.d("onItemClick : " , String.valueOf(position));
                System.out.println(context);
                imageView = (ImageView) view;
                final Picture_tool picture_tool = new Picture_tool();
                picture_tool.init(context, imageView);
                picture_tool.dialog.show();
            }
        });
    }


    class ViewHolder {
        public NineGridlayout ivMore;
    }

    class Adapter extends NineGridAdapter {

        public Adapter(Context context, List list) {
            super(context, list);
        }

        @Override
        public int getCount() {
            return (list == null) ? 0 : list.size();
        }

        @Override
        public String getUrl(int position) {
            return getItem(position) == null ? null : ((Image)getItem(position)).getUrl();
        }

        @Override
        public Object getItem(int position) {
            return (list == null) ? null : list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int i, View view) {
            if (view != null && view instanceof ImageView) {
                imageView = (ImageView) view;
            } else {
                imageView = new ImageView(context);
            }
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setBackgroundColor(context.getResources().getColor((android.R.color.transparent)));
            String url = getUrl(i);
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.login_pic)		;		//加载成功之前占位图
            Glide.with(context).asBitmap().load(getUrl(i)).apply(options).into(imageView);
            //mImageView.setImageBitmap(data);
//
//            if (!TextUtils.isEmpty(url)) {
//                imageView.setTag(url);
//            }
            return imageView;
        }
    }
}
